var left_margin_step = 15;

// when #ectypename element is clicked, show ectype refpanel with the first level ectype
$("#ectypename").live(
		'click',
		function() {
			var myts = new Date();
			var formStr = $('#ectype_filter').serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context + "/register/getNextLevelEctype?myts="
						+ myts,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
					if (result != null && result != "[]") {
						var orgArr = eval(result);
						$("#ecTypePicker").find(".ectype_tree_list").empty();
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];
							var a = $("<a></a>").addClass("ectype_category_trigger")
									.attr("margin", 0);
							var inputObj = $("<input/>").val(obj.pk_defdoc)
									.attr("type", "radio").attr("name",
											"pk_defdoc_intree");
							var label = $("<label></label>").text(obj.name);
							var div = $("<div></div>")
									.addClass("ectype_tree_category").append(a)
									.append(inputObj).append(label);
							$("#ecTypePicker").find(".ectype_tree_list").append(div);
						}
					}
				}
			});
			$("#ecTypePicker").slideDown("normal");
			var position = $("#ectypename").offset();
			position.top += 20;
			$("#ecTypePicker").offset(position);
		}
);

// when icon (+) is clicked, expand and show next level ectypes;
$(".ectype_category_trigger").live(
		'click',
		function() {
			var category_triggerObj=$(this);
			if(category_triggerObj.hasClass("close")){
				category_triggerObj.parent(".ectype_tree_category").next(".children").slideUp(function(){
					category_triggerObj.removeClass("close");
				});
				return false;
			}
			$(this).addClass("close");
			var pk_parent = category_triggerObj.next().val();
			var myts = new Date();
			var formStr = $('#ectype_filter').serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context
						+ "/register/getNextLevelEctype?parentid="+pk_parent+"&myts=" + myts,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
					if(result == "[]"){
						return false;
					}
					var orgArr = eval(result);
					var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
					var childrenDiv = $("<div></div>").addClass("children");
					for ( var loop = 0; loop < orgArr.length; loop++) {
						var obj = orgArr[loop];

						var a = $("<a></a>").addClass("ectype_category_trigger")
						        .attr("style","margin-left:"+current_margin_left_size+"px").attr("margin", current_margin_left_size);
						var inputObj = $("<input/>").val(obj.pk_defdoc)
								.attr("type", "radio").attr("name","pk_defdoc_intree");
						var label = $("<label></label>").text(obj.name);
						
						var div = $("<div></div>")
							.addClass("ectype_tree_category").append(a).append(inputObj).append(label);
						childrenDiv.append(div);
					}
					childrenDiv.insertAfter(category_triggerObj.parent());
				}
			});
			category_triggerObj.parent(".ectype_tree_category").siblings(".ectype_tree_category").next(".children").slideUp(function(){
				$(this).prev(".ectype_tree_category").find(".ectype_category_trigger").removeClass("close");
			});
			return false;
});

//when filter name, get from top level doc
$("#filterEcType").live(
		'click',
		function() {
			var myts = new Date();
			var formStr = $('#ectype_filter').serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context + "/register/getNextLevelEctype?myts="
						+ myts,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
					if (result != null && result != "[]") {
						var orgArr = eval(result);
						$("#ecTypePicker").find(".ectype_tree_list").empty();
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];
							var a = $("<a></a>").addClass("ectype_category_trigger")
									.attr("margin", 0);
							var inputObj = $("<input/>").val(obj.pk_defdoc)
									.attr("type", "radio").attr("name",
											"pk_defdoc_intree");
							var label = $("<label></label>").text(obj.name);
							var div = $("<div></div>")
									.addClass("ectype_tree_category").append(a)
									.append(inputObj).append(label);
							$("#ecTypePicker").find(".ectype_tree_list").append(div);
						}
					}
				}
			});
});

//confirm
$("#ecTypePicked").live('click', function(){
	var items = $("#ecTypePicker").find("[name='pk_defdoc_intree']");
	if(items != null){
		for(var loop=0; loop<items.length; loop++){
			if(items[loop].checked == true){
				var pk_defdoc = $(items[loop]).val();
				var name = $(items[loop]).next("label").html();
				$("#ectypename").val(name).attr("style","color:black;cursor:pointer");
				$("#ectype").val(pk_defdoc);
			}
		}
	}
	$("#ecTypePicker").slideUp();
});

//cancel 
$("#ecTypeCancel").live('click', function() {
	$("#ecTypePicker").slideUp();
	return false;
});